# $Id: time2.tcl 1520 2008-10-17 17:16:09Z sergei $
# Replies to XEP-0202 (Entity Time) queries

custom::defvar options(reply_xmpp_time) 1 \
    [::msgcat::mc "Reply to entity time (urn:xmpp:time) requests."] \
    -group IQ -type boolean

proc xmpp_time {xlib from child args} {
    variable options

    if {!$options(reply_xmpp_time)} {
	return {error cancel service-unavailable}
    }

    set curtime [clock seconds]
    set restags \
	[list [::xmpp::xml::create utc \
		    -cdata [clock format $curtime \
				  -format "%Y-%m-%dT%TZ" -gmt true]] \
	      [::xmpp::xml::create tzo \
		    -cdata [timezone_offset]]]
    
    set res [::xmpp::xml::create time \
		    -xmlns urn:xmpp:time \
		    -subelements $restags]
    
    return [list result $res]
}

proc timezone_offset {} {
    set H [clock format 0 -format %H]
    set M [clock format 0 -format %M]
    set S +

    if {$H > 12} {
	set H [expr {24 - $H}]
	set M [expr {60 - $M}]
	set S -
    }

    return $S$H:$M
}

::xmpp::iq::register get time urn:xmpp:time [namespace current]::xmpp_time

